package com.yingpai.fitness.dvp_2_0.main.fragment.home;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.NestedScrollView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.yingpai.fitness.R;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.callback.Callback;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.request.RequestType;
import com.yingpai.fitness.dvp_2_0.common.fragment.CommonFragment;
import com.yingpai.fitness.dvp_2_0.common.util.constant.Contacts;
import com.yingpai.fitness.dvp_2_0.common.view.statusbar.Status;
import com.yingpai.fitness.dvp_2_0.common.view.statusbar.StatusView;
import com.yingpai.fitness.dvp_2_0.main.activity.mine.AllOrderListActivity;
import com.yingpai.fitness.dvp_2_0.main.activity.mine.HelpActivity;
import com.yingpai.fitness.dvp_2_0.main.activity.mine.InviteFriendActvity;
import com.yingpai.fitness.dvp_2_0.main.activity.mine.MyClassActivity;
import com.yingpai.fitness.dvp_2_0.main.activity.mine.MyCouponActivity;
import com.yingpai.fitness.dvp_2_0.main.activity.mine.MyDynamicActivity;
import com.yingpai.fitness.dvp_2_0.main.activity.mine.MyFanOrFollowActivity;
import com.yingpai.fitness.dvp_2_0.main.activity.mine.PersonalDetailActivity;
import com.yingpai.fitness.dvp_2_0.main.activity.mine.SettingActivity;
import com.yingpai.fitness.dvp_2_0.main.activity.mine.SignActivity;
import com.yingpai.fitness.dvp_2_0.main.activity.mine.assmble.AssmbleOrderListActivity;
import com.yingpai.fitness.dvp_2_0.main.activity.mine.integral.IntegralMallActivity;
import com.yingpai.fitness.dvp_2_0.main.activity.mine.shop_refund.RefundServiceListActivity;
import com.yingpai.fitness.dvp_2_0.main.bean.MineBean;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Constant;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Url;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import de.hdodenhof.circleimageview.CircleImageView;
import q.rorbin.badgeview.QBadgeView;

/**
 * Created by ASUS on 2018/11/2.
 */

public class MineFragment extends CommonFragment implements OnRefreshLoadMoreListener {

    @BindView(R.id.status_view)
    StatusView statusView;
    @BindView(R.id.my_user_img_iv)
    CircleImageView myUserImgIv;
    @BindView(R.id.info_rl)
    RelativeLayout info_rl;
    @BindView(R.id.userNameTv)
    TextView userNameTv;
    @BindView(R.id.versionTv)
    TextView versionTv;
    @BindView(R.id.nested_scroll_view)
    NestedScrollView nestedScrollView;
    @BindView(R.id.userInfoRl)
    LinearLayout userInfoRl;
    @BindView(R.id.no_login)
    TextView no_login;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.myPublish_num)
    TextView myPublish_num;
    @BindView(R.id.my_follow_num)
    TextView my_follow_num;
    @BindView(R.id.my_fans_num)
    TextView my_fans_num;
    //    @BindView(R.id.sign)
//    TextView sign;
    Unbinder unbinder;
    @BindView(R.id.obligation_tv)
    TextView obligationTv;
    @BindView(R.id.myPendDeliveryTv)
    TextView myPendDeliveryTv;
    @BindView(R.id.myPendReceiptTv)
    TextView myPendReceiptTv;
    @BindView(R.id.myPendEvaluateTv)
    TextView myPendEvaluateTv;
    @BindView(R.id.myServiceTv)
    TextView myServiceTv;
    Unbinder unbinder1;

    public static MineFragment newInstance() {
        return new MineFragment();
    }

    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dvp_2_0_fragment_mine, container, false);
        unbinder = ButterKnife.bind(this, view);
        EventBus.getDefault().register(this);
        refreshLayout.setOnRefreshLoadMoreListener(this);
        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setRefreshHeader(new MaterialHeader(context).setColorSchemeResources(R.color.common_color));

//        ViewGroup.MarginLayoutParams rl = (ViewGroup.MarginLayoutParams) info_rl.getLayoutParams();
////        int marginPx = DensityUtils.dp2px(context, 24);
////        rl.setMargins(marginPx, DensityUtils.dp2px(context, 85) - statusView.sStatusBarHeight, marginPx, marginPx);
////        info_rl.requestLayout();

        nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                int headerHeight = userInfoRl.getHeight();
                int scrollDistance = Math.min(scrollY, headerHeight);
                int statusAlpha = (int) ((float) scrollDistance / (float) headerHeight * 255F);
                setDrawerLayoutBarAlpha(statusAlpha);
            }
        });

        Status.with(context)
                .navigationBarBackground(ContextCompat.getDrawable(context, R.color.colorNavigation))
                .invasionStatusBar();

        setDrawerLayoutBarAlpha(0);

        versionTv.setText("v" + getAppVersionName());

        return view;
    }

    protected void setDrawerLayoutBarAlpha(int alpha) {
        statusView.getBackground().mutate().setAlpha(alpha);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

    @Override
    protected void initData() {
        finalOkGo.requestEncrypt(RequestType.POSTENCRYPT, Contacts.HOST_SERVER + Url.MINE_DATA, null,
                Url.MINE_DATA, new Callback<MineBean>() {
            @Override
            public void onSuccess(MineBean mineBean) {
                if (mineBean.getCode().equals(Constant.REQUEST_SUCCESS_0)) {
                    no_login.setVisibility(View.GONE);
                    userNameTv.setText(mineBean.getData().getName());
                    Glide.with(context)
                            .load(mineBean.getData().getPhotoUrl())
                            .error(R.mipmap.icon_user_default)
                            .centerCrop()
                            .into(myUserImgIv);
                    myPublish_num.setText(mineBean.getData().getMyTheme() + "");
                    my_follow_num.setText(mineBean.getData().getFollow() + "");
                    my_fans_num.setText(mineBean.getData().getFans() + "");
                    badgeView(obligationTv, mineBean.getData().getUnPaid());
                    badgeView(myPendDeliveryTv, mineBean.getData().getUnSend());
                    badgeView(myPendReceiptTv, mineBean.getData().getUnReceive());
                    badgeView(myPendEvaluateTv, mineBean.getData().getUnComment());
                    badgeView(myServiceTv, mineBean.getData().getRefound());
                } else {
                    showToast(mineBean.getMessage() + "");
                }
                refreshLayout.finishRefresh();
            }
        });
    }

    private void badgeView(TextView textView, String string) {
        new QBadgeView(context)
                .bindTarget(textView)
//                .setBadgeTextSize(20, false)
                .setBadgeTextColor(getResources().getColor(R.color.white))
                .setBadgeGravity(Gravity.END | Gravity.TOP)
                .setGravityOffset(3, 3, true)
                .setBadgeBackground(getResources().getDrawable(R.drawable.oval_badge_view))
                .setBadgeNumber(Integer.parseInt(string));
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    @Override
    @OnClick({R.id.sign, R.id.my_user_img_iv, R.id.myAllOrderRl, R.id.obligation_tv,
            R.id.myPendDeliveryTv, R.id.myPendReceiptTv, R.id.myPendEvaluateTv,
            R.id.myServiceTv, R.id.myPublishTv, R.id.my_follow_tv_flag,
            R.id.my_fans_tv_flag, R.id.myClassRl, R.id.integral_mall,
            R.id.my_coupon, R.id.ProblemRl, R.id.customRl, R.id.setRl,
            R.id.invite_rl, R.id.my_assmble})
    public void onViewClicked(View view) {
        super.onViewClicked(view);
        switch (view.getId()) {
            case R.id.sign:
                startActivity(SignActivity.class);
                break;
            case R.id.my_user_img_iv:
                startActivity(PersonalDetailActivity.class);
                break;
            case R.id.myAllOrderRl:
                Bundle bundle = new Bundle();
                bundle.putInt("flag", 0);
                startActivity(AllOrderListActivity.class, bundle);
                break;
            case R.id.obligation_tv:
                Bundle bundle1 = new Bundle();
                bundle1.putInt("flag", 1);
                startActivity(AllOrderListActivity.class, bundle1);
                break;
            case R.id.myPendDeliveryTv:
                Bundle bundle2 = new Bundle();
                bundle2.putInt("flag", 2);
                startActivity(AllOrderListActivity.class, bundle2);
                break;
            case R.id.myPendReceiptTv:
                Bundle bundle3 = new Bundle();
                bundle3.putInt("flag", 3);
                startActivity(AllOrderListActivity.class, bundle3);
                break;
            case R.id.myPendEvaluateTv:
                Bundle bundle4 = new Bundle();
                bundle4.putInt("flag", 4);
                startActivity(AllOrderListActivity.class, bundle4);
                break;
            case R.id.myServiceTv:
                startActivity(RefundServiceListActivity.class);
                break;
            case R.id.myPublishTv:
                startActivity(MyDynamicActivity.class);
                break;
            case R.id.my_follow_tv_flag:
                Bundle bundle5 = new Bundle();
                bundle5.putString("title", "我的关注");
                startActivity(MyFanOrFollowActivity.class, bundle5);
                break;
            case R.id.my_fans_tv_flag:
                Bundle bundle6 = new Bundle();
                bundle6.putString("title", "我的粉丝");
                startActivity(MyFanOrFollowActivity.class, bundle6);
                break;
            case R.id.invite_rl:
                startActivity(InviteFriendActvity.class);
                break;
            case R.id.myClassRl:
                startActivity(MyClassActivity.class);
                break;
            case R.id.integral_mall:
                startActivity(IntegralMallActivity.class);
                break;
            case R.id.my_assmble:
                startActivity(AssmbleOrderListActivity.class);
                break;
            case R.id.my_coupon:
                startActivity(MyCouponActivity.class);
                break;
            case R.id.ProblemRl:
                startActivity(HelpActivity.class);
                break;
            case R.id.customRl:
                call(Constant.phoneNum);
                break;
            case R.id.setRl:
                startActivity(SettingActivity.class);
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.POSTING)
    public void OnMessageEvent(String s) {
        if (Constant.UPDATA_INFOMATION.equals(s)) {
            initData();
        }
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {

    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        initData();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder1 = ButterKnife.bind(this, rootView);
        return rootView;
    }
}
